package tree.medium;

import tree.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * 114. 二叉树展开为链表
 */
public class flatten {
    public void flatten(TreeNode root) {
        List<TreeNode> list = new ArrayList<>();
        dfs(root, list);
        int size = list.size();
        for (int i = 1; i < size; i++) {
            TreeNode prev = list.get(i - 1), curr = list.get(i);
            prev.left = null;
            prev.right = curr;
        }
    }
    public void dfs(TreeNode treeNode, List<TreeNode> treeNodes) {
        if (treeNode == null) {
            return;
        }
        treeNodes.add(treeNode);
        dfs(treeNode.left, treeNodes);
        dfs(treeNode.right, treeNodes);
    }
}
